import {
    CreateChatDialog,
    GetDialogInfo,
    GetLeastDialogInfo,
    GetListChatContent
} from "../../wailsjs/go/dbservice/DbChatDialogService";
import {RespCodeOk} from "../constant/enums/RespCode";
import {FindOneById} from "../../wailsjs/go/dbservice/UserToolService";
import {ToastError, ToastInfo} from "../utils/Toast";
import {dbdto} from "../../wailsjs/go/models";
import {useWindowsStore, WindowsType} from "../store/windows";
import {useGlobalStore} from "../store/global";
import {GetNumber} from "../../wailsjs/go/dbservice/DbConfigService";
import {ConfigKeyDefaultToolId} from "../constant/config_keys";

//进入最新的对话
export function EntryLeastDialog(){
        GetLeastDialogInfo().then(dialogInfo=>{
            if(dialogInfo){
                 console.log(dialogInfo," ---dialogInfo")
                EntryChatDialog(dialogInfo.id)
            }else{
                ToastError("获取最新对话失败")
            }
        })
}


//进入某个对话
export function EntryChatDialog(DialogId:number){
    let storeGlobal=useGlobalStore()
    let storeWin=useWindowsStore()
    GetDialogInfo(DialogId).then(DialogInfo=>{
           let  ToolId = DialogInfo.tool_id
           let  ClipArr = DialogInfo.clips.split("【+】")
             //获取对话的消息列表
            GetListChatContent(DialogId,false).then(ContentListRes=>{

                if(ContentListRes.code == RespCodeOk){ //获取成功
                    //获取tool信息
                    FindOneById(ToolId).then(ToolInfo=>{
                        if(!ToolInfo){
                            ToastError("获取tool失败")
                            return
                        }

                        //storeGlobal.value.SetToolInfo(toolRes as dbdto.UserToolInfo)

                        //开始组装数据并添加到message
                        let list=ContentListRes.data as Array<dbdto.ChatContentInfo>
                        console.log("list",ToolInfo,list)
                        for (let i = 0; i < list.length; i++) {
                            let item=list[i]
                            let data={
                                clipboard_texts:ClipArr,
                                tool:ToolInfo as dbdto.UserToolInfo,
                                info:item,
                            }

                            //添加到消息框
                            storeGlobal.MessageAddContent(data)
                        }

                        //切换窗体
                        if(storeWin.CurrWindow !=WindowsType.Message ){
                            storeWin.ChangeWindow(WindowsType.Message)
                        }
                    })

                    //console.log("EntryDialog" ,list)
                }else{
                    ToastError(ContentListRes.msg)
                }
            })
    })
}


export function NewChatDialog(){
    let storeGlobal=useGlobalStore()
    GetNumber(ConfigKeyDefaultToolId).then(ToolId=>{
        if(ToolId  <= 0) ToolId=storeGlobal.CurrChatToolId
        if(ToolId  <= 0){
            ToastError("当前无对话记录")
            return
        }

        CreateChatDialog(ToolId,[]).then(res=>{
            if(res.code == RespCodeOk){
                storeGlobal.CurrChatDialogId=res.data
                storeGlobal.CurrMessages=[]
                ToastInfo("已创建新对话")
            }else{
                ToastError("清空异常："+res.msg)
            }
        })
    })


}